package com.employment.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.employment.dao.JobPositionMapper;
import com.employment.entity.JobPosition;
import com.employment.service.JobPositionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

// TableDef将在编译时自动生成

/**
 * 职位管理服务实现类
 * 
 * @author Employment System
 * @since 1.0.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class JobPositionServiceImpl implements JobPositionService {

    private final JobPositionMapper jobPositionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createJob(JobPosition jobPosition) {
        try {
            // 设置初始状态
            jobPosition.setStatus(0); // 0-待发布
            jobPosition.setViewCount(0);
            jobPosition.setApplyCount(0);
            
            return jobPositionMapper.insert(jobPosition) > 0;
        } catch (Exception e) {
            log.error("创建职位失败: ", e);
            throw new RuntimeException("创建职位失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateJob(JobPosition jobPosition) {
        try {
            return jobPositionMapper.update(jobPosition) > 0;
        } catch (Exception e) {
            log.error("更新职位失败: ", e);
            throw new RuntimeException("更新职位失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteJob(Long jobId) {
        try {
            return jobPositionMapper.deleteById(jobId) > 0;
        } catch (Exception e) {
            log.error("删除职位失败: ", e);
            throw new RuntimeException("删除职位失败");
        }
    }

    @Override
    public JobPosition getJobById(Long jobId) {
        return jobPositionMapper.selectOneById(jobId);
    }

    @Override
    public Page<JobPosition> getJobPage(Page<JobPosition> page,
                                       String keyword,
                                       String category,
                                       String city,
                                       BigDecimal salaryMin,
                                       BigDecimal salaryMax,
                                       String workExperience,
                                       String education,
                                       Integer workType,
                                       Integer status) {
        return jobPositionMapper.selectJobPage(page, keyword, category, city,
                salaryMin, salaryMax, workExperience, education, workType, status);
    }

    @Override
    public List<JobPosition> getJobsByCompanyId(Long companyId, Integer status) {
        return jobPositionMapper.selectByCompanyId(companyId, status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean publishJob(Long jobId) {
        try {
            JobPosition jobPosition = jobPositionMapper.selectOneById(jobId);
            if (jobPosition == null) {
                throw new RuntimeException("职位不存在");
            }
            
            jobPosition.setStatus(1); // 1-招聘中
            jobPosition.setPublishTime(LocalDateTime.now());
            
            return jobPositionMapper.update(jobPosition) > 0;
        } catch (Exception e) {
            log.error("发布职位失败: ", e);
            throw new RuntimeException("发布职位失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean offlineJob(Long jobId) {
        try {
            JobPosition jobPosition = jobPositionMapper.selectOneById(jobId);
            if (jobPosition == null) {
                throw new RuntimeException("职位不存在");
            }
            
            jobPosition.setStatus(0); // 0-下线
            
            return jobPositionMapper.update(jobPosition) > 0;
        } catch (Exception e) {
            log.error("下线职位失败: ", e);
            throw new RuntimeException("下线职位失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean increaseViewCount(Long jobId) {
        try {
            return jobPositionMapper.increaseViewCount(jobId) > 0;
        } catch (Exception e) {
            log.error("增加浏览次数失败: ", e);
            return false;
        }
    }

    @Override
    public List<JobPosition> getHotJobs(Integer limit) {
        return jobPositionMapper.selectHotJobs(limit);
    }

    @Override
    public List<JobPosition> getRecommendJobs(Long userId, Integer limit) {
        return jobPositionMapper.selectRecommendJobs(userId, limit);
    }

    @Override
    public List<String> getJobStats() {
        return jobPositionMapper.selectJobStats();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateStatus(List<Long> jobIds, Integer status) {
        try {
            // 使用简单的SQL查询
            for (Long jobId : jobIds) {
                JobPosition jobPosition = jobPositionMapper.selectOneById(jobId);
                if (jobPosition != null) {
                    jobPosition.setStatus(status);
                    jobPositionMapper.update(jobPosition);
                }
            }
            return true;
        } catch (Exception e) {
            log.error("批量更新职位状态失败: ", e);
            throw new RuntimeException("批量更新职位状态失败");
        }
    }
}